package 代码随想2._5双指针法;

/**
 * @author XXX
 * @date 2024-01-17 15:42
 */

import 代码随想2._2链表.ListNode;

/**
 * https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/
 */
public class _6删除链表的倒数第N个节点 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode newhead = new ListNode(-1);
        newhead.next = head;

        ListNode fast = newhead;
        ListNode slow = newhead;

        for (int i=0;i<n;i++)
            fast = fast.next;

        while (fast.next!=null){
            fast = fast.next;
            slow =slow.next;
        }
        slow.next = slow.next.next;
        return newhead.next;
    }
}
